SELECT COUNT(1) as total FROM (
SELECT  /*+FIRST_ROWS*/ DISTINCT
	T .MNO,
	T .in_mno,
	T .CPR_REG_NM_CN,
	b.RAT1,
	T .DT_CTE,
	T .MEC_STS,
	T .AGRNO,
	T .MEC_TYP,
	T .CPR_OPER_NM_CN,
	T .IS_STAR,
	t1.CONT_NM_CN,
	t1.CONT_TEL_NO,
	t1.CONT_TEL_NO_ENC,
	t1.SIGN_DT,
	t1.SIGN_END_DT,
	t1.mec_admin_tel,
	t1.mec_admin_tel_enc,
	D .AREA_NM,
	ue.EMP_NM,
	uo.org_Nm FIRST_NM,
	uob.org_Nm,
	uob.PARENT_ORG,
	uobr.org_Nm BRANCH_NM,
	b.mobile_oper_typ,
	T .MEC_SS_AUTH_STS,
	T .mec_dis_nm,
	T .REGION_CODE_MAINT,
	T .COOPERATION_MANAGER_ID,
	T .BRANCH_ORG_UUID,
	T .AGENT_BRANCH_ORG_UUID,
	T .ROOT_AGENT_ORG_NO,
	T .AGENT_ORG_NO,
	T .BUSINESS_EMP_NO,
	T .IS_XW_MEC,
	T .SYS_ID,
	whuob.org_Nm branchOrgNm,
	MANAGE .org_Nm manager_Nm,
	TO_CHAR (
		cmt.create_time,
		'yyyy-mm-dd'
	) AS weChatTime,
	cmt.status AS weChatStatus
FROM
	dcm_owner.T_BAP_MEC_IF T
LEFT JOIN dcm_owner.T_BAP_MEC_RATE b ON (
	T .in_mno = b.in_mno
	AND b.atv_flg = '00'
)
LEFT JOIN dcm_owner.T_BAP_MEC_IF_DETAIL t1 ON t1.in_mno = T .in_mno
LEFT JOIN dcm_owner.t_bap_cde_area D ON T .MEC_CITY_CD = TO_CHAR (D .AREA_ID)
LEFT JOIN dcm_owner.t_uap_org uo ON T .ROOT_AGENT_ORG_NO = uo.org_uuid
LEFT JOIN dcm_owner.t_uap_org whuob ON T .BRANCH_ORG_UUID = whuob.org_uuid
LEFT JOIN dcm_owner.t_uap_org uob ON T .AGENT_ORG_NO = uob.org_uuid
LEFT JOIN dcm_owner.t_uap_org uobr ON T .AGENT_BRANCH_ORG_UUID = uobr.org_uuid
LEFT JOIN dcm_owner.t_uap_org MANAGE ON T .COOPERATION_MANAGER_ID = MANAGE .org_uuid
LEFT JOIN dcm_owner.T_UAP_EMPLOYEE ue ON (
	ue.active_flg = 1
	AND ue.emp_no = T .BUSINESS_EMP_NO
)
LEFT JOIN (
	SELECT
		mt.merc_no,
		MIN (mt.create_time) create_time,
		MIN (mt.status) status
	FROM
		dcm_owner.T_MSS_WECHAT_MERC_BIND mt
	GROUP BY
		mt.merc_no
) cmt ON T .mno = cmt.merc_no
WHERE
	<![CDATA[ T .mec_sts <> '00' ]]> 
	
	<!--分公司条件  -->
	<if test="branchOrgNo!=null and branchOrgNo!='' ">
		and uobr.ORG_UUID=#{branchOrgNo}
	</if>
	<if test="mecTyp!=null and mecTyp=='02'">
		and t.MEC_TYP=#{mecTyp}
	</if>
	<if test="mecTyp!=null and mecTyp=='00'">
		and t.MEC_TYP IN ('00','01')
	</if>
	<if test="mno!=null and mno!=''">
		and t.MNO = #{mno}
	</if>
	<if test="cprRegNmCn!=null and cprRegNmCn!=''">
		and t.CPR_REG_NM_CN like #{cprRegNmCn}||'%'
	</if>
	<if test="cprOperNmCn!=null and cprOperNmCn!=''">
		and t.CPR_OPER_NM_CN like '%'||#{cprOperNmCn}||'%'
	</if>
	<if test="orgNm!=null and orgNm!=''">
		and uob.ORG_NM like '%'||trim(#{orgNm})||'%'
	</if>
	<if test="managerNm!=null and managerNm!=''">
		and manage.ORG_NM like '%'||trim(#{managerNm})||'%'
	</if>
	<if test="mecSts!=null and mecSts!=''">
		and t.Mec_Sts =#{mecSts}
	</if>
	<if test="agrno!=null and agrno!=''">
		and t.AGRNO = #{agrno}
	</if>
	<if test="mecProvCd!=null and mecProvCd!=''">
		and t.MEC_PROV_CD = #{mecProvCd}
	</if>
	<if test="mecCityCd!=null and mecCityCd!=''">
		and t.MEC_CITY_CD = #{mecCityCd}
	</if>
	<if test="contTelNo!=null and contTelNo!=''">
		and t1.CONT_TEL_NO_ENC = #{contTelNo}
	</if>
	<if test="rat1!=null and rat1!=''">
		and b.RAT1 = #{rat1}
	</if>
	<if test="openEndTm!=null and openEndTm!=''">
		and t1.SIGN_DT &lt;= #{openEndTm}
	</if>
	<if test="openStartTm!=null and openStartTm!=''">
		and t1.SIGN_DT &gt;= #{openStartTm}
	</if>
	<if test="signEndTm!=null and signEndTm!=''">
		and t1.SIGN_END_DT &lt;= #{signEndTm}
	</if>
	<if test="signStartTm!=null and signStartTm!=''">
		and t1.SIGN_END_DT &gt;= #{signStartTm}
	</if>
	<if test="mercOprMbl!=null and mercOprMbl!=''">  
		and t1.MEC_ADMIN_TEL = #{mercOprMbl}
	</if>
	<if test="mercOprMblEnc!=null and mercOprMblEnc!=''">  
		and t1.MEC_ADMIN_TEL_ENC = #{mercOprMblEnc}
	</if>
	<if test="creEndTm!=null and creEndTm!=''">
		and t.DT_CTE &lt;= #{creEndTm}
	</if>
	<if test="creStartTm!=null and creStartTm!=''">
		and t.DT_CTE &gt;= #{creStartTm}
	</if>   
	<if test="orgNo!=null and orgNo!='' and orgList!=null ">
		and t.AGENT_ORG_NO in 
		<foreach item="item" index="index" collection="orgList" open="("
		separator="," close=")">
		#{item}
	   </foreach>
	</if>
	<if test="isStar!=null and isStar!=''">
		and t.IS_STAR = #{isStar}
	</if>
	<if test="empNm!=null and empNm!=''">
		and ue.EMP_NM like '%'||#{empNm}||'%'
	</if>

	<if test="mobileOperTyp != null and mobileOperTyp != ''">
		and b.mobile_oper_typ = #{mobileOperTyp}
	</if>
	<if test="mecDisNm != null and mecDisNm != ''">
		and t.mec_dis_nm like '%'||#{mecDisNm}||'%'
	</if>
	<if test="mercTrdDesc != null and mercTrdDesc != ''">
		and  b.IDT_TYP=#{mercTrdDesc}
	</if>
	<!--进件类型  -->
	<if test="isXwMec != null and isXwMec != '' and isXwMec == '01'"  >
		and t.IS_XW_MEC =#{isXwMec}
	</if>
	<if test="isXwMec != null and isXwMec != '' and isXwMec != '01'"  >
		and (t.IS_XW_MEC =#{isXwMec} or t.IS_XW_MEC is null)
	</if>
	<!--进件渠道  -->
	<if test="sysId != null and sysId != '' and sysId == '00'">
		and t.SYS_ID ='APP'
	</if>
	<if test="sysId != null and sysId != '' and sysId == '02'">
		and t.SYS_ID ='XINLM'
	</if>
	<if test="sysId != null and sysId != '' and sysId == '01'">
		and (t.SYS_ID !='APP' and t.SYS_ID !='XINLM')
	</if>
	
	<if test="weChatStartTime != null and weChatStartTime != '' ">
		<![CDATA[ and to_char(cmt.CREATE_TIME,'yyyymmdd') >= #{weChatStartTime} ]]>
	</if>
	<if test="weChatEndTime != null and weChatEndTime != '' ">
		<![CDATA[ and to_char(cmt.CREATE_TIME,'yyyymmdd') <= #{weChatEndTime} ]]>
	</if>
	<if test="weChatStatus != null and weChatStatus != '' ">
		<if test="weChatStatus == '-1'">
			and cmt.status is null
		</if>
		<if test="weChatStatus != '-1'">
			and cmt.status = #{weChatStatus}
		</if>
	</if>
	<![CDATA[ 
		AND b.EXP_DT >= TO_CHAR(SYSDATE, 'YYYYMMDD')
		AND b.EFF_DT <= TO_CHAR(SYSDATE, 'YYYYMMDD')
		AND (t1.SIGN_END_DT    IS NULL
		OR t1.SIGN_END_DT      >= TO_CHAR(SYSDATE, 'YYYYMMDD'))
		AND (t1.SIGN_DT        IS NULL
		OR t1.SIGN_DT          <= TO_CHAR(SYSDATE, 'YYYYMMDD'))
		AND (b.merc_typ_rate in ('01','06'))
		 
	]]>
	<if test="topAgentOrgNm != null and topAgentOrgNm != ''">
		and (SELECT ORG_NM FROM dcm_owner.T_UAP_ORG A WHERE A.PARENT_ORG IS NULL
		START WITH A.ORG_UUID = D.ORG_NO CONNECT BY  PRIOR A.PARENT_ORG =  A.ORG_UUID ) LIKE '%${topAgentOrgNm}%'  
	</if>
	order by t.dt_cte
	desc
	)